package com.cmc.cloud.cmclink.doc.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cmc.cloud.cmclink.doc.entity.BlnoSendEdoLogDO;
import com.cmc.cloud.cmclink.doc.mapper.BlnoSendEdoLogMapper;
import com.cmc.cloud.cmclink.doc.service.BlnoSendEdoLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 发送edo记录
 *
 * @author 招商轮船-管理员
 */
@Service
public class BlnoSendEdoLogServiceImpl implements BlnoSendEdoLogService {

    @Resource
    private BlnoSendEdoLogMapper blnoSendEdoLogMapper;

    private static String VALID = "0";
    private static String NOT_VALID = "1";

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public boolean writeSendEdoLog(BlnoSendEdoLogDO blnoSendEdoLogDo) {
        //每次先更新同一个提单号下其他日志为无效，记录最新的发送日志
        LambdaUpdateWrapper<BlnoSendEdoLogDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BlnoSendEdoLogDO::getBlno,blnoSendEdoLogDo.getBlno());
        updateWrapper.eq(BlnoSendEdoLogDO::getValid,VALID);
        updateWrapper.set(BlnoSendEdoLogDO::getValid,NOT_VALID);
        blnoSendEdoLogMapper.update(null,updateWrapper);
        blnoSendEdoLogMapper.insert(blnoSendEdoLogDo);
        return true;
    }
}
